package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.GroupNoticeReceive;
import cn.stylefeng.guns.sys.modular.system.mapper.GroupNoticeReceiveMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.GroupNoticeReceiveParam;
import cn.stylefeng.guns.sys.modular.system.model.result.GroupNoticeReceiveResult;
import cn.stylefeng.guns.sys.modular.system.service.GroupNoticeReceiveService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 群通接收表 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-07-26
 */
@Service
public class GroupNoticeReceiveServiceImpl extends ServiceImpl<GroupNoticeReceiveMapper, GroupNoticeReceive> implements GroupNoticeReceiveService {

    @Override
    public void add(GroupNoticeReceiveParam param) {
        GroupNoticeReceive entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(GroupNoticeReceiveParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(GroupNoticeReceiveParam param) {
        GroupNoticeReceive oldEntity = getOldEntity(param);
        GroupNoticeReceive newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public GroupNoticeReceiveResult findBySpec(GroupNoticeReceiveParam param) {
        return null;
    }

    @Override
    public List<GroupNoticeReceiveResult> findListBySpec(GroupNoticeReceiveParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(GroupNoticeReceiveParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData saveNoticeReceicedInfo(GroupNoticeReceiveParam param) {

        GroupNoticeReceiveParam param2=new GroupNoticeReceiveParam();
        param2.setNoticeId(param.getNoticeId());
        param2.setPortalUserId(param.getPortalUserId());
        List<GroupNoticeReceiveResult> resultList=this.baseMapper.customList(param2);
        //已经看过的不用再保存一次，更新时间即可
        if(CollectionUtil.isNotEmpty(resultList)){
            GroupNoticeReceive entity = new GroupNoticeReceive();
            ToolUtil.copyProperties(resultList.get(0), entity);
            this.updateById(entity);
            return ResponseData.success();
        }
        GroupNoticeReceive entity = getEntity(param);
        entity.setUpdateTime(new Date());
        this.save(entity);
        return ResponseData.success();
    }

    private Serializable getKey(GroupNoticeReceiveParam param) {
        return param.getReceiveId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private GroupNoticeReceive getOldEntity(GroupNoticeReceiveParam param) {
        return this.getById(getKey(param));
    }

    private GroupNoticeReceive getEntity(GroupNoticeReceiveParam param) {
        GroupNoticeReceive entity = new GroupNoticeReceive();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
